package com.springboot.service.impl;

import com.springboot.bean.User;
import com.springboot.bean.UserInfo;
import com.springboot.mapper.OperationRecordMapper;
import com.springboot.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Converter;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.springboot.bean.Student;
import com.springboot.mapper.StudentMapper;
import com.springboot.service.StudentService;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Service("studentService")
@Slf4j
public class StudentServiceImp implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private OperationRecordMapper operationRecordMapper;
//    private static final Logger logger = LogManager.getLogger(StudentServiceImp.class);
    @Override
    public int add(Student student) {
        return this.studentMapper.add(student);
    }

    @Override
    public int update(Student student) {
        return this.studentMapper.update(student);
    }

    @Override
    public int deleteBysno(String sno) {
        return this.studentMapper.deleteBysno(sno);
    }

    @Override
    public Student queryStudentBySno(String sno) {
        return this.studentMapper.queryStudentBySno(sno);
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectAll();
    }

    @Override
    public UserInfo selectUser(String id) {
        log.info("{}查询用户信息{}",id,userMapper.selectUser(Long.valueOf(id)));
        log.warn("{}查询用户信息{}",id,userMapper.selectUser(Long.valueOf(id)));
        log.error("{}查询用户信息{}",id,userMapper.selectUser(Long.valueOf(id)));
        log.debug("{}查询用户信息{}",id,userMapper.selectUser(Long.valueOf(id)));
        return this.userMapper.selectUser(Long.valueOf(id));
    }

    @Override
    public UserInfo updateUser(UserInfo user) {
        this.userMapper.updateUser(user);
        Object value = user.getId();
        Long num = 1L;
        if (value != null && value instanceof String) {
            String str = (String) value;
            num = Long.valueOf(str);
        } else if (value instanceof Long) {
            num = (Long) value;

        } else {
            System.out.println("数据类型未知");
        }
        // 记录日志，帮助排查
        System.out.println("获取到的是 Long 类型数据: " + num);
        return this.userMapper.selectUser(num);
    }

    @Override
    public int deleteUser(String id) {
        return this.userMapper.deleteUser(id);
    }

    @Override
    public int addUserInfo(UserInfo user) {
        return this.operationRecordMapper.insertUser(user);
    }

    @Override
    @Async("asyncThreadPoolTaskExecutor")
    public void asyncMethod() {
        sleep();
        log.info("异步方法内部线程名称：{}", Thread.currentThread().getName());
    }

    @Override
    public void syncMethod() {
        sleep();
    }
    private void sleep() {
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
